<template>
  <div class="page-box">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <TowerElect v-model="state.dataForm.stationId" placeholder="所属杆塔" clearable></TowerElect>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.name" placeholder="相机名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <ren-select v-model="state.dataForm.type" dict-type="camera_type" :placeholder="$t('notice.type')"
                    clearable></ren-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <!--      <el-form-item>-->
      <!--        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button v-if="state.hasPermission('KxCamera:kxcamera:save')" type="primary" @click="addOrUpdateHandle()">
          {{ $t("add") }}
        </el-button>
      </el-form-item>
      <!--      <el-form-item>-->
      <!--        <el-button v-if="state.hasPermission('KxCamera:kxcamera:delete')" type="danger" @click="state.deleteHandle()">-->
      <!--          {{ $t("deleteBatch") }}-->
      <!--        </el-button>-->
      <!--      </el-form-item>-->
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
              @selection-change="state.dataListSelectionChangeHandle" :row-class-name="tableRowClassName"
              style="width: 100%">
      <!--      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
      <el-table-column prop="name" label="相机名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="stationName" label="所属杆塔" header-align="center" align="center"></el-table-column>
      <el-table-column prop="type" label="相机类型" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("camera_type", scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="gbDeviceNo" label="设备编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="gbChannelNo" label="通道编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="ip" label="ip地址" header-align="center" align="center"></el-table-column>
      <el-table-column prop="isOnline" label="是否在线" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("is_online", scope.row.isOnline) }}
        </template>
      </el-table-column>
      <!--      <el-table-column prop="gbFlv" label="国标播放地址" header-align="center" align="center"></el-table-column>-->
      <!--      <el-table-column prop="nvrId" label="nvrid" header-align="center" align="center"></el-table-column>-->
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('KxCamera:kxcamera:update')" type="primary" link
                     @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}
          </el-button>
          <el-button v-if="state.hasPermission('KxCamera:kxcamera:delete')" type="primary" link
                     @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {nextTick, reactive, ref, toRefs, watch} from "vue";
import {tableRowClassName} from "@/utils/tableRowClassName";
import AddOrUpdate from "./kxcamera-add-or-update.vue";
import TowerElect from "@/components/tower-elect.vue";


const view = reactive({
  tableRowClassName,
  getDataListURL: "/KxCamera/kxcamera/page",
  getDataListIsPage: true,
  exportURL: "/KxCamera/kxcamera/export",
  deleteURL: "/KxCamera/kxcamera",
  deleteIsBatch: true,
  dataForm: {
    stationId: "",
    name: "",
    type: ""
  }
});

const state = reactive({...useView(view), ...toRefs(view)});


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
</script>
